package com.zhj.interview;

/**
 * ThreadLocal 测试
 */
public class Test18 {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    private static ThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) {
        inheritableThreadLocal.set("hello main");
        new Thread(()-> {
            try {
                threadLocal.set("hello");
                System.out.println( Thread.currentThread().getName() + threadLocal.get());
                System.out.println( Thread.currentThread().getName() + inheritableThreadLocal.get());
                inheritableThreadLocal.set("hello thread1");
                System.out.println( Thread.currentThread().getName() + inheritableThreadLocal.get());
            } finally {
                threadLocal.remove();
            }
        },"thread1 - ").start();

        new Thread(()-> {
            try {
                Thread.sleep(10);
                System.out.println( Thread.currentThread().getName() + inheritableThreadLocal.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                threadLocal.remove();
            }
        }, "thread2 - ").start();
    }
}
